package yxy.game.pm2.active.command.daygift;

import org.joda.time.DateTime;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.packagedata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_每日礼包;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.message.PayMessage;
import yxy.game.pm2.message.PayMessage.GetDayGiftRequest;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.List;

@MessageType(proto = Command.GetDayGift, classtype = PayMessage.GetDayGiftRequest.class)
public class CMD_Active_每日礼包_DrawReward extends Command<GetDayGiftRequest> {

	@Override
	public void execute(MessageSource messageSource, GetDayGiftRequest request) throws Exception {
		String pcode = messageSource.getTag();
		PayMessage.GetDayGiftResponse.Builder response = PayMessage.GetDayGiftResponse.newBuilder();

		ActiveModule_每日礼包 am = ActiveModule_每日礼包.get(pcode,false);

		boolean daygift = am.cangetdaygift();
		if (!daygift) {
			send(messageSource, response, Result.ActHadGet);
			return;
		}

		packagedata pd = DataCenter.getData(20, packagedata.class);
		if (pd == null) {
			send(messageSource, response, Result.DataError);
			return;
		}

		List<Currency> curs = new ArrayList<Currency>();

		int[] rewardtypes = pd.getRewardtype();
		int[] rewardids = pd.getRewardid();
		int[] rewardnums = pd.getRewardnum();

		curs = (List<Currency>) Currency.create(rewardtypes, rewardids, rewardnums);

		am.gaindayagift(DateTime.now());
		CurrencyHandle ch = new CurrencyHandle();
		ch.addCurrency(pcode, CCReason.活动_每日礼包, curs);

		for (int i = 0; i < curs.size(); i++) {
			PackageMessage.Item.Builder item = PackageMessage.Item.newBuilder();

			Currency c = curs.get(i);
			item.setType(c.getType().getNumber());
			item.setDataId(c.getDataId());
			item.setNumber(c.getNumber());
			response.addDisplay(item.build());
			response.addUpdate(item.build());
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, PayMessage.GetDayGiftResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}

}
